package com.pdstudio.weather;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pdstudio.app.Application;
import com.pdstudio.bean.ArticleSearchList;
import com.pdstudio.bean.WeatherProfile;
import com.pdstudio.bean.WeatherStamp;
import com.pdstudio.http.LiveHttpMethod;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.view.SwipeBackActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ScienceActivity extends SwipeBackActivity {

	private ListView lvScience;
	private SimpleAdapter scienceAdapter;
	private Handler handler = null;
	private TextView txtTitle;
	private View customSunTitleView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_science);

		if (true) {// TODO 这里判断天气情况
			LinearLayout layout = (LinearLayout) findViewById(R.id.activity_science);
			layout.setBackground(getResources().getDrawable(R.drawable.bgsun));
		}

		customSunTitleView = findViewById(R.id.title_science);
		txtTitle = (TextView) customSunTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("气象科普");

		lvScience = (ListView) findViewById(R.id.scinece_list);

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					LiveHttpMethod http = new LiveHttpMethod(
							ScienceActivity.this);
					List<ArticleSearchList> list = new ArrayList<ArticleSearchList>();
					list = http.GetArticleList(Application.getInstance().getSharePreferenceUtil().getAuthCode(), Application.getInstance().getCurSimpleCity().cityCode);
					Message msg = handler.obtainMessage(0, list);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {
					if (msg.what == 0) {
						Toast.makeText(getApplicationContext(), "数据加载中", 0)
								.show();
						// msg.obj是获取handler发送信息传来的数据
						@SuppressWarnings("unchecked")
						List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

						if (msg != null && msg.obj != null) {
							List<ArticleSearchList> searchList = (List<ArticleSearchList>) msg.obj;

							for (ArticleSearchList o : searchList) {
								Map<String, Object> map = new HashMap<String, Object>();
								map.put("articleId", o.getArticleId());
								map.put("title", o.getTitle());
								map.put("postTime", o.getPostTime());
								list.add(map);
							}
						}
						// 给ListView绑定数据
						set(list);
					}
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void set(List<Map<String, Object>> list) {
		scienceAdapter = new SimpleAdapter(this, list,
				R.layout.listview_item_sicence, new String[] { "articleId",
						"title", "postTime" }, new int[] { R.id.science_id,
						R.id.science_title, R.id.science_date });
		lvScience.setAdapter(scienceAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.science, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
